package com.smartcar.driver.service.impl;

import com.smartcar.common.domain.User;
import com.smartcar.common.domain.request.FindUserVo;
import com.smartcar.common.domain.request.OrderRequest;
import com.smartcar.common.result.Result;
import com.smartcar.driver.common.domain.DriverFile;
import com.smartcar.driver.common.domain.request.DriverOrderVo;
import com.smartcar.driver.common.domain.request.FindFileVo;
import com.smartcar.driver.feign.AdminFeign;
import com.smartcar.driver.feign.AdminFeignServiceTemLate;
import com.smartcar.driver.feign.OrderFeignServiceTemLate;
import com.smartcar.driver.mapper.DriverMapper;
import com.smartcar.driver.service.DriverService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

@Service
public class DriverServiceImpl implements DriverService {

    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private OrderFeignServiceTemLate orderFeignServiceTemLate;

    @Autowired
    private AdminFeignServiceTemLate adminFeignServiceTemLate;

    @Autowired
    private RedissonClient redissonClient;

    @Override
    public DriverFile findDriverFileByUserNumber(FindFileVo findFileVo) {
        return driverMapper.findDriverFileByUserNumber(findFileVo);
    }

    @Override
    /*@Transactional*/
    public int scrambleForOrder(DriverOrderVo driverOrderVo) {
        RLock lock = redissonClient.getLock("scrambleForOrder" + driverOrderVo.getOrderNumber());
        try {
            lock.tryLock(10, 30, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 订单填入司机状态
        OrderRequest orderRequest = OrderRequest.builder()
                .orderNumber(driverOrderVo.getOrderNumber())
                .orderState(1)
                .driverstartTime(new Date())
                .orderstartTime(new Date())
                .driverName(driverOrderVo.getNickName())
                .driverPhone(driverOrderVo.getDriverPhone()).build();
        // 修改司机接单状态
        User user = User.builder()
                .orderNumber(driverOrderVo.getDriverNumber())
                .acceptStatus(1)
                .build();
        int i = 0;
        int i1 = 0;
        try {
            // 添加司机信息 到 对应 抢到的 订单上
            i = orderFeignServiceTemLate.insertOrderByDriver(orderRequest);
            // 修改司机状态并添加当前定位
            i1 = adminFeignServiceTemLate.updateDriverAcceptStatus(user);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // 判断当前线程是否持有该锁
        if (lock.isHeldByCurrentThread()) {
            // 判断是否任务是否都执行成功 --》 释放锁条件
            if (i > 0 && i1 > 0) {
                lock.unlock();
            } else {
                // 不满足释放锁的条件，可以选择不释放或抛出异常等处理方式
                // 这里选择抛出异常表示释放失败
                // 再次尝试释放锁
                lock.unlock();
                throw new RuntimeException("释放锁失败");
            }
        } else {
            // 当前线程不持有该锁，无需进行释放操作
            throw new RuntimeException("当前线程不持有该锁，无需进行释放操作");
        }
        return i1;
    }
}
